
import axios from '@/utils/request'
/**
 * 获取所有频道
 * @returns  Promise对象
 */
export const getListAPI = () => {
  return axios({
    url: '/v1_0/channels'
  })
}
/**
 * 设置频道列表
 * @param {*} channel {id:频道id,sep:频道索引} 注意传参时对象要加{}
 * @returns Promise对象
 */
export const upLoadUserChannelAPI = ({ channels }) =>
  axios({
    url: '/v1_0/user/channels',
    method: 'put',
    data: {
      channels
    }
  })

/**
 *登录
 * @param {*} param0 {mobile:手机号，code：验证码}
 * @returns  Promise对象
 */
export const LoginAPI = ({ mobile, code }) => {
  return axios({
    url: '/v1_0/authorizations',
    method: 'post',
    data: {
      mobile,
      code
    }
  })
}

/**
 * 获取用户已选频道
 * @returns Promise对象
 */
export const getUserChannelListAPI = () => {
  return axios({
    url: '/v1_0/user/channels'
  })
}

/**
 *获取所有文章
 * @param {*} param0 { channel_id:频道id, timestamp 上一段文章中最后一个文章的时间戳, 用于获取下一页数据, 注意时间可以自己在控制台运行(new Date()).getTime()试试}
 * @returns Promise对象
 */
export const getArticleListAPI = ({ channel_id, timestamp }) => {
  return axios({
    url: '/v1_0/articles',
    params: {
      channel_id,
      timestamp
    }
  })
}

/**
 * 反馈不感兴趣
 * @param {*} art_id 文章id
 * @returns Promise对象
 */
export const getDislikeAPI = (art_id) => {
  return axios({
    url: '/v1_0/article/dislikes',
    method: 'post',
    data: {
      target: art_id
    }
  })
}

/**
 *举报
 * @param {*} param0 {art_id:文章id type:举报类型, remark：其他问题}
 * @returns Promise对象
 */
export const getArtReportAPI = ({ art_id, type, remark }) => {
  const artObj = {
    target: art_id,
    type
  }
  if (type === 0) {
    artObj.remark = remark
  }
  return axios({
    url: '/v1_0/article/reports',
    method: 'post',
    data: artObj
  })
}
/**
 * 获取搜索联想
 * @param {*} param0 keywords:输入框的关键字
 * @returns Promise对象
 */
export const getSuggestionAPI = ({ keywords }) =>
  axios({
    url: '/v1_0/suggestion',
    params: {
      q: keywords
    }
  })
/**
 * 获取搜索结果列表
 * @param {*} param0 page:页码 per_page：每页显示条数, q：关键词字符串
 * @returns Promise对象
 */
export const getSearchReultListAPI = ({ page, per_page, q }) => {
  return axios({
    url: '/v1_0/search',
    params: {
      page,
      per_page,
      q
    }
  })
}
/**
 * 获取文章详情
 * @param {*} param0  artId:文章id
 * @returns Promise对象
 */
export const getArticleDetailAPI = ({ artId }) => {
  return axios({
    url: `/v1_0/articles/${artId}`
  })
}
// 获取已关注
export const followedUserAPI = ({ target }) => {
  return axios({
    url: '/v1_0/user/followings',
    method: 'post',
    data: {
      target
    }
  })
}
// 文章 - 取消关注作者
export const unFollowedUserAPI = ({ uid }) => {
  return axios({
    // 这uid只是个变量名, 把值拼接在url发给后台(无需指定参数名)
    url: `/v1_0/user/followings/${uid}`,
    method: 'DELETE'
  })
}
// 文章 - 点赞
export const articleLikeAPI = ({ target }) => {
  return axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: target
    }
  })
}

// 文章 - 取消点赞
export const articleDisLikeAPI = ({ artId }) => {
  return axios({
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE'
  })
}

// 文章评论列表
export const getArticleCommentListAPI = ({ art_id, offset = null, limit = 10 }) => {
  return axios({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: art_id,
      offset,
      limit
    }
  })
}
// 评论 - 喜欢
export const commentLikingAPI = ({ comId }) => {
  return axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}
// 评论-取消喜欢
export const commentDisLikingAPI = ({ comId }) => {
  return axios({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}
// 新增评论
export const addSendCommentAPI = ({ comId, sendText }) => {
  return axios({
    url: '/v1_0/comments',
    method: 'post',
    data: {
      target: comId,
      content: sendText
    }
  })
}
// 获取 用户信息
export const getUserInfoAPI = () =>
  axios({
    url: '/v1_0/user'
  })
// 获取用户简介
export const getUserProfileAPI = () =>
  axios({
    url: '/v1_0/user/profile'
  })

/**
 *、设置头像
 * @param {*} fd FormData格式
 */
export const uploadUserImgAPI = fd =>
  axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: fd
  })
/**
 * 更新用户信息
 * @param {*} param0 data 有才传递 没有传空 只用params会自动判断data不能
 * @returns
 */
export const patchUserProfileAPI = ({ name = '', gender = '', integer = '', birthday = '', intro = '' }) => {
  const dataObj = {
    name,
    gender,
    integer,
    birthday,
    intro
  }
  const newObj = {}
  for (const key in dataObj) {
    if (dataObj[key]) {
      // 遍历dataObj每一个key和value 有就放到newObj
      newObj[key] = dataObj[key]
    }
  }

  return axios({
    url: '/v1_0/user/profile',
    method: 'patch',
    data: newObj
  })
}
